package com.easy.dfc.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.Utility;
import com.easy.core.web.BaseController;
import com.easy.dfc.entity.*;
import com.easy.dfc.service.ActivityService;
import com.easy.dfc.service.UsersetService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "${apiPath}/Activity")
public class ActivityController extends BaseController {


    @Resource
    private ActivityService activityService;

    @Resource
    private UsersetService usersetService;

    /**
     * 活动报名(天天特价/聚爆款/疯抢/拼团/跳尾货)
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityList", method = RequestMethod.POST)
    public Result ActivityList(@RequestBody ParaActivity para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return activityService.ActivityList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名(天天特价/聚爆款/疯抢/拼团/跳尾货)
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollAdd", method = RequestMethod.POST)
    public Result ActivityEnrollAdd(@RequestBody ParaEnroll para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            if(Utility.isNullorEmpty(para.getFactoryId())
                    || Utility.isNullorEmpty(para.getActivityId())
                    || Utility.isNullorEmpty(para.getProductId())){
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }
            return activityService.ActivityEnrollAdd(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名(订货会)
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollAdd1", method = RequestMethod.POST)
    public Result ActivityEnrollAdd1(@RequestBody ParaEnroll1 para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return activityService.ActivityEnrollAdd1(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 活动报名列表
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollList", method = RequestMethod.POST)
    public Result ActivityEnrollList(@RequestBody ParaEnrollQuery para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return activityService.ActivityEnrollList(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名列表
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollList1", method = RequestMethod.POST)
    public Result ActivityEnrollList1(@RequestBody ParaEnrollQuery para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return activityService.ActivityEnrollList1(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 报名撤回
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollCancel", method = RequestMethod.POST)
    public Result ActivityEnrollCancel(@RequestBody ParaEnrollQuery para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return activityService.ActivityEnrollCancel(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名详情
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollDetail", method = RequestMethod.POST)
    public Result ActivityEnrollDetail(@RequestBody ParaEnrollQuery para, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            return activityService.ActivityEnrollDetail(para);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名付款
     * @param para
     * @param moDel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollPay", method = RequestMethod.POST)
    public Result ActivityEnrollPay(@RequestBody ParaPayInfo para, HttpServletRequest request, Model moDel) {
        try {
            if (!usersetService.VerifyToken(para.getUserId(), para.getToken())) {
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            StringBuffer URL = request.getRequestURL();
            String url = URL.substring(0,URL.length()-10);
            //StringBuffer URL = "http://"+request.getRemoteHost()+":"+request.getRemotePort()+"/"+request.getContextPath()+"/";

            return activityService.ActivityEnrollPay(para, url);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 支付完成回调
     * @param aliNotify
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "aliNotify")
    public String aliNotify(AliNotify aliNotify, HttpServletRequest request, Model model) {
        try {
            //boolean signVerified = SignUtils.rsaCheckV1(paramsMap, ALIPAY_PUBLIC_KEY, "UTF-8"); //调用SDK验证签名
            //if(signVerfied){
            //  验签成功后
            //按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure
            //}else{
            //  验签失败则记录异常日志，并在response中返回failure.
            //}
            return activityService.aliNotify(aliNotify);
        } catch (Exception e) {
            logger.error(e.toString());
            return "fail";
        }
    }

}
